package com.Map_Practice;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapDemo {
	static Map<Integer, Student> map = new HashMap<Integer, Student>();

	// 以下方法为序列化对象方法，将对象保存在文件之中
	public static void serialize() throws Exception {
		File file = new File("E:/io/test/student.txt");

		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		OutputStream outputFile = new FileOutputStream(file);
		ObjectOutputStream cout = new ObjectOutputStream(outputFile);
		Iterator<Integer> it = map.keySet().iterator();
		// 获取迭代器 ,也可map.values();
		while (it.hasNext()) {
			Object key = it.next();
			cout.writeObject(map.get(key));
		}

		//cout.writeObject(map);   //方法二 直接存入Map
		cout.writeObject(null);
		cout.close();
		outputFile.close();
	}

	// 以下方法为反序列化对象方法，从文件中读取已经保存的对象
	public static void deserialize() throws Exception {
		File file = new File("E:/io/test/student.txt");
		InputStream inputFile = new FileInputStream(file);
		ObjectInputStream cin = new ObjectInputStream(inputFile);
		System.out.println(file.length());

		Object obj;
		while ((obj = cin.readObject()) != null) {
			Student stu = (Student) obj;
			map.put(stu.getId(), stu);

		}

		cin.close();
		inputFile.close();
	}

	//方法二 直接读取Map
	/*
	 * public static void deserialize() throws Exception { 
	 * File file = new File("E:/io/test/student.txt");
	 * InputStream inputFile = new FileInputStream(file);
	 *  ObjectInputStream cin = new ObjectInputStream(inputFile);
	 * 
	 * map = (Map)cin.readObject();
	 * 
	 * cin.close(); inputFile.close(); 
	 * }
	 */

	static public Result add(Student stu) {
		map.put(stu.getId(), stu);
		Result result = new Result();
		result.success("恭喜添加成功！");

		return result;
	}

	static public Result add(String name, int id, double score) {

		Student stu = new Student();
		stu.setName(name);
		stu.setId(id);
		stu.setScore(score);

		return add(stu);
	}

	/**
	 * 删除指定ID的学生
	 */
	static public Result Delete(int id) {
		Student stu = find(id);
		Result result = new Result();
		if (stu != null) {
			map.remove(id);
			return result.success("恭喜您，删除学员成功");
		} else {
			return result.fail("学员不存在");
		}
	}

	/**
	 * 查找指定ID的学生
	 * 
	 * @return Student | null
	 */
	static public Student find(int id) {
		if (map.isEmpty())
			return null;
		return map.get(id);

	}

	/**
	 * 修改指定学号学生的成绩/姓名
	 */
	static public Result Edit(int id, double score, String name) {
		Student student = new Student();
		student.setId(id);
		student.setScore(score);
		student.setName(name);
		return Edit(student);
	}

	/**
	 * 修改指定学号学生的所有信息
	 */
	static public Result Edit(Student student) {
		Student stu = find(student.getId());
		Result result = new Result();

		if (stu == null) {
			return result.fail("该学生不存在，不能进行编辑");
		} else {

			if (student.getName() != null && !"".equals(student.getName())) {
				stu.setName(student.getName());
			}
			if (student.getScore() >= 0) {
				stu.setScore(student.getScore());
			}
		}
		return result.success("修改成功！");
	}

	/**
	 * 打印所有学生信息
	 */
	static public void showAll() {
		Iterator<Integer> it = map.keySet().iterator();
		// 获取迭代器
		while (it.hasNext()) {
			Object key = it.next();
			System.out.println(map.get(key));
		}
	}
}
